<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-alert title="添加修改会删除缓存，请谨慎操作" type="warning" show-icon>
      </el-alert>
      <el-row :gutter="20" class="goodsbut">
        <el-col :span="6">
          <el-input
            placeholder="请输入商品名称"
            clearable
            @clear="getGoodsList()"
            v-model="queryPageInfo.goodsName"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList()"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="3">
          <el-select
          @click="getGoodsList"
          @change="getGoodsList" v-model="queryPageInfo.goodsState" placeholder="请选择商品状态">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="3">
          <el-select @change="getGoodsList" v-model="queryPageInfo.goodsSelect" placeholder="请选择商品选择分类">
            <el-option
              v-for="item in goodsSelectList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-col>
      </el-row>
      <el-row :gutter="20" class="goodsbut">
        <el-col :span="2.5">
          <el-button  type="primary" @click="addGoodsShow"
            >添加商品</el-button
          >
        </el-col>
        <el-col :span="2.5">
          <el-button  type="primary" @click="addGoodsIndex"
            >添加索引库</el-button
          >
        </el-col>
        <el-col :span="2.5">
          <el-button  type="primary" @click="updateGoodsSearch"
            >更新es数据</el-button
          >
        </el-col>
      </el-row>
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index" label=""></el-table-column>
        <el-table-column prop="name" label="名称"></el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryPageInfo.pageNum"
        :page-sizes="[1, 5, 10, 20]"
        :page-size="queryPageInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getGoodsSelectList } from '@/api/goodsSelect'
import { admingGetGoodsData, addGoodsIndexData, updateGoodsSearchData } from '@/api/goods'
export default {
  name: 'GoodsList',
  data () {
    return {
      queryPageInfo: {
        goodsName: '',
        goodsState: '',
        goodsSelect: '',
        pageNum: 1,
        pageSize: 10
      },
      goodsList: [],
      goodsSelectList: [],
      options: [
        {
          value: '1',
          label: '已启用'
        },
        {
          value: '0',
          label: '未启用'
        }
      ],
      total: 0
    }
  },
  methods: {
    addGoodsShow () {
      this.$router.push('goodsAdd')
    },
    async getGoodsList () {
      const res = await admingGetGoodsData(this.queryPageInfo)
      this.goodsList = res.data.list
      this.total = res.data.total
    },
    handleSizeChange (newPageSize) {
      this.queryPageInfo.pageSize = newPageSize
      this.getGoodsList()
    },
    handleCurrentChange (newPageNum) {
      this.queryPageInfo.pageNum = newPageNum
      this.getGoodsList()
    },
    async getGoodsSelectData () {
      const res = await getGoodsSelectList()
      this.goodsSelectList = res.data
    },
    async addGoodsIndex () {
      const res = await addGoodsIndexData()
      this.$message.success(res.data)
    },
    async updateGoodsSearch () {
      const res = await updateGoodsSearchData()
      this.$message.success(res.data)
    }
  },
  created () {
    this.getGoodsSelectData()
    this.getGoodsList()
  }
}
</script>

<style scoped lang="less">
.goodsbut {
  margin-top: 20px;
}
</style>
